package com.gable.report.controller;

import com.gable.report.common.util.DES;
import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.dao.IReportAuthDao;
import com.gable.report.model.ChartsBaseDataView;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.ReportAuth;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalBaseDataService;
import com.gable.report.service.SetRepeatRepairService;
import com.gable.report.service.UserService;
import com.gable.report.util.ChartsUtil;
import com.sun.istack.internal.logging.Logger;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by lvdouStart on 2017/3/22.
 */
@Controller
@RequestMapping("/charts/hospital/department-index")
public class ChartsForHospitalDepartmentIndexController {

    Logger logger = Logger.getLogger(ChartsForHospitalDepartmentIndexController.class);

    @Resource
    HttpServletRequest request;

    @Resource
    HttpServletResponse response;

    @Autowired
    UserService userService;

    @Autowired
    SetRepeatRepairService setRepeatRepairService;

    @Autowired
    ChartsForHospitalBaseDataService chartsForHospitalBaseDataService;

    @Autowired
    IReportAuthDao reportAuthDao;


    /** 
     * @Author: 杨青桦
     * @Description:  用户权限校验
     * @Date: 2017/8/15 11:16
     * @param userId
     * @return
     */
    @RequestMapping(value="/check-login")
    public String checkUser(String userId, Model model){
        if(userId == null || "".equals(userId)){
            return "charts-authority";
        }
        userId = userId.replace(" ","+");
        User loginUser;
        try {
            byte [] byt =Base64.decodeBase64(userId);
            byte[] bytes = DES.decrypt(byt, DES.password);
            userId = new String(bytes);
            loginUser =  userService.queryHospitalById(Long.valueOf(userId));
            if(loginUser == null){
                return "charts-authority";
            }
            //报表未开通
            ReportAuth reportAuth = reportAuthDao.queryByHospitalId(loginUser.getHospitalId());
            if(reportAuth == null || reportAuth.getReportTag() == 0){
                return "charts-authority";
            }

            //校验成功后 设置重复维修
            setRepeatRepairService.setIfRepeatRepair(loginUser.getHospitalId());
            HttpSession session = request.getSession();				// 创建session对象
            session.setAttribute("loginUser", loginUser);
            model.addAttribute("userId",userId);
            model.addAttribute("hospitalId",loginUser.getHospitalId());
            //无权限查看运营管理
            if(reportAuth.getOperationTag() == 0){
                model.addAttribute("auth",0);
                return "/charts/hospital/department-index";
            }else{
                model.addAttribute("auth",1);
                return "/charts/hospital/overview";
            }
        }catch (Exception e) {
            e.printStackTrace();
            return "charts-authority";
        }


    }


    @RequestMapping(value="/page", method= RequestMethod.GET)
    public String page(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        return ChartsUtil.checkAuthority(user,chartsHospitalQuery) ? "charts/hospital/department-index" : "charts-authority";
    }



    /**
     * @Author: 杨青桦
     * @Description:  主页总览概览框接口
     * @Date: 2017/7/19 16:04
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllAssets(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryAllAssetsForDepartmentIndex(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:  主维度 -- 图表
     * @Date: 2017/7/19 16:30
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/department",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDepartments(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {

            map = chartsForHospitalBaseDataService.queryDepartments(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description:  设备分类和型号
     * @Date: 2017/7/19 16:30
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/type",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypes(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceTypes(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 年份数据
     * @Date: 2017/7/20 11:42
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/year-trend",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryYearTrend(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map = new HashMap<>();
        try {
            List<ChartsBaseDataView> list = chartsForHospitalBaseDataService.queryYearTrend(hospitalId, chartsHospitalQuery);
            map.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }

    /**
     * @Author: 杨青桦
     * @Description: "➕"总览具体设备信息
     * @Date: 2017/7/20 11:42
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceInfo(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceInfoByModelName(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /** 
     * @Author: 杨青桦
     * @Description: 所有具体设备详细信息接口
     * @Date: 2017/9/6 14:14
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/assets",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryAllDevices(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryDeviceInfoByModelName(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


    /**
     * @Author: 杨青桦
     * @Description: 查询新增或者报废设备信息
     * @Date: 2017/12/20 10:47
     * @param chartsHospitalQuery
     * @return
     */
    @RequestMapping(value="/new-or-scrap",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryNewlyOrScrapAssetsInfo(ChartsHospitalQuery chartsHospitalQuery){
        User user = (User)request.getSession().getAttribute("loginUser");
        Long hospitalId = ChartsUtil.getHospitalId(user,chartsHospitalQuery);
        Map<String,Object> map;
        try {
            map = chartsForHospitalBaseDataService.queryNewlyOrScrapAssetsInfo(hospitalId, chartsHospitalQuery);
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"查询成功",map);
    }


}
